//
//  HELExamListTableView.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/2/17.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELExamListTableView.h"
#import "HELMaterialCell.h"
#import "HELExamModel.h"
#import "HELExameRecordModel.h"
#import "HELPrepareForExamVC.h"
#import "HELExamWebVC.h"
@interface HELExamListTableView ()
@property (nonatomic,  assign) BOOL isFirstGetData;

@property (nonatomic,  assign) NSUInteger page;

@end

@implementation HELExamListTableView



- (instancetype)init
{
    self = [super init];
    if (self) {
        self.examVCType = HELExamListTypeExamList;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.isFirstGetData = YES;
    // Do any additional setup after loading the view.
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64- 49);
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"HELMaterialCell" bundle:nil] forCellReuseIdentifier:@"HELMaterialCell"];
    self.tableView.separatorStyle= UITableViewCellSeparatorStyleNone;

    weakSelf_define;
    
    [self.tableView wsj_HeaderRefreshBlock:^{
        if (weakSelf.examVCType ==  0) {
             [weakSelf requestExameData:YES];
        }else{
             [weakSelf requestExameRecorde:YES];
        }
       
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
        if (weakSelf.examVCType ==  0) {
            [weakSelf requestExameData:NO];
        }else{
            [weakSelf requestExameRecorde:NO];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestDataFirst{
    if (self.isFirstGetData) {
        if (self.examVCType ==  0) {
            [self requestExameData:YES];
        }else{
            [self requestExameRecorde:YES];
        }
        self.isFirstGetData = NO;
    }
}


- (void)requestExameRecorde:(BOOL)isRecover{
    
    
    if (isRecover) {
        self.page = 1;
    }else{
        self.page ++ ;
    }
    
    self.tableView.loading = YES;
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    params[@"rows"] = @(10);
    
    [[HELNetworkManager sharedMg] GET:helExamRecord parameters:params success:^(id responseObject) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if (isRecover) {
            [weakSelf.dataArray removeAllObjects];
        }
        if (responseObject[@"success"] && [responseObject[@"success"] integerValue]) {
            
            NSInteger totalPage = 0;
            if (![responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
              totalPage = [responseObject[@"data"][@"totalPage"] integerValue];
            }else{
                totalPage = 0;
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELExameRecordModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            [weakSelf.tableView reloadData];
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        
    }];


}


- (void)requestExameData:(BOOL)isRecover{
    
    
    if (isRecover) {
        self.page = 1;
    }else{
        self.page ++ ;
    }
    
    self.tableView.loading = YES;
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    params[@"rows"] = @(10);
   
    [[HELNetworkManager sharedMg] GET:helMyExamUrl parameters:params success:^(id responseObject) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if (isRecover) {
            [weakSelf.dataArray removeAllObjects];
        }
        if (responseObject[@"success"] && [responseObject[@"success"] integerValue]) {
            
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage = [responseObject[@"data"][@"totalPage"] integerValue];
            }
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELExamModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            [weakSelf.tableView reloadData];
        }
     
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        
    }];
    
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELMaterialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELMaterialCell"];
    if (self.examVCType == HELExamListTypeExamList) {
         cell.model = self.dataArray[indexPath.row];
    }else{
          cell.record_model = self.dataArray[indexPath.row];
    }
   
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.examVCType == HELExamListTypeExamList) {
        HELExamModel *model = self.dataArray[indexPath.row];
        HELPrepareForExamVC *vc =[[HELPrepareForExamVC alloc] initWithExamId:model.id];
        [self.parentViewController.navigationController pushViewController:vc animated:YES];
    }else{
        HELExameRecordModel *model = self.dataArray[indexPath.row];
        HELExamWebVC *vc =[HELExamWebVC new];
      
        NSString * url = [NSString stringWithFormat:@"%@/%@/%@/%@", [HELNetworkManager  sharedMg].bathURL,helExamRecordUrl,@(2),model.id];
        vc.theUrl =[NSURL URLWithString:url];
        [self.parentViewController.navigationController pushViewController:vc animated:YES];
        
      
    }
    
    
}


@end
